﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Project.Tool
{
	public class ApplicationHelper
    {
		/// <summary>
		/// 添加 全局数据缓存
		/// </summary>
		/// <param name="key">关键字</param>
		/// <param name="value">缓存数据</param>
		/// <returns></returns>
		public static bool Add(string key, object value)
		{
			try
			{
				if ( string.IsNullOrWhiteSpace(key) || value == null )
					return false;

				HttpContext.Current.Application.Lock();
				HttpContext.Current.Application.Add(key, value);
				HttpContext.Current.Application.UnLock();

				return true;
			}
			catch ( Exception e )
			{
				return false;
			}
		}

		/// <summary>
		/// 修改 全局数据缓存
		/// </summary>
		/// <param name="key">关键字</param>
		/// <param name="value">缓存数据</param>
		/// <returns></returns>
		public static bool Set(string key, object value) {

			try
			{
				if ( string.IsNullOrWhiteSpace(key) || value == null )
					return false;

				HttpContext.Current.Application.Lock();
				HttpContext.Current.Application.Set(key, value);
				HttpContext.Current.Application.UnLock();

				return true;
			}
			catch(Exception e) {
				return false;
			}
		}

		/// <summary>
		/// 读取缓存数据
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static object Get(string key)
		{
			return HttpContext.Current.Application.Get(key);
		}
	}
}
